package com.cxh.rmsq.ui.adapter.self

import android.content.Intent
import android.widget.TextView
import androidx.core.view.isVisible
import com.blankj.utilcode.util.TimeUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.cxh.rmsq.R
import com.cxh.rmsq.common.IntentExtra
import com.cxh.rmsq.db.model.CommunityContent
import com.cxh.rmsq.ui.activity.self.CommentDetailActivity
import com.cxh.rmsq.ui.activity.self.LookUserDetailActivity2
import com.cxh.rmsq.ui.widget.SelectableRoundedImageView
import com.cxh.rmsq.utils.loadImage


/**
 *
 */
class LookMeListAdapter(type: Int) :
    BaseQuickAdapter<CommunityContent, BaseViewHolder>(R.layout.item_lookme_list),
    LoadMoreModule {

    var type = type  //1谁看过我 2我看谁

    override fun convert(holder: BaseViewHolder, item: CommunityContent) {
        with(holder) {

            setText(R.id.tv_name, item.nickname)
            setText(R.id.tv_sex_age, item.age.toString())

            if (1 == item.sex) {
                holder.getView<TextView>(R.id.tv_sex_age).setCompoundDrawablesWithIntrinsicBounds(
                    context.getResources().getDrawable(R.mipmap.icon_male),
                    null,
                    null,
                    null
                )
                holder.getView<TextView>(R.id.tv_sex_age)
                    .setBackgroundResource(R.drawable.shape_sex_male_bg)
            } else {
                holder.getView<TextView>(R.id.tv_sex_age).setCompoundDrawablesWithIntrinsicBounds(
                    context.getResources().getDrawable(R.mipmap.icon_male),
                    null,
                    null,
                    null
                )
                holder.getView<TextView>(R.id.tv_sex_age)
                    .setBackgroundResource(R.drawable.shape_sex_female_bg)
            }

            if (type == 1) {
                holder.getView<TextView>(R.id.tv_sex_age).isVisible = true
                setText(
                    R.id.tv_content,
                    TimeUtils.getFriendlyTimeSpanByNow(item.createTime) + " 查看过我的资料"
                )
                setText(R.id.tv_count, item.hisCount + "次")
            } else if (type == 2) {
                holder.getView<TextView>(R.id.tv_sex_age).isVisible = true
                setText(
                    R.id.tv_content,
                    TimeUtils.getFriendlyTimeSpanByNow(item.createTime) + " 我看过ta的资料"
                )
                setText(R.id.tv_count, item.hisCount + "次")
            } else if (type == 3) {//点赞
                var str = ""
                if (item.type == 1) {//1动态2评论
                    str = "赞了你的动态"
                } else {
                    str = "赞了你的评论"
                }
                holder.getView<TextView>(R.id.tv_sex_age).isVisible = false
                setText(
                    R.id.tv_content, str + "\n" +
                            TimeUtils.getFriendlyTimeSpanByNow(item.createTime)
                )
                setText(R.id.tv_count, item.content)
            } else if (type == 4) {//评论
                var str = ""
                if (item.type == 1) {// 1：评论 2：回复
                    str = "评论了你:" + item.content
                } else {
                    str = "回复了你:" + item.content
                }

                holder.getView<TextView>(R.id.tv_sex_age).isVisible = false
                setText(
                    R.id.tv_content, str + "\n" +
                            TimeUtils.getFriendlyTimeSpanByNow(item.createTime)
                )
                setText(R.id.tv_count, item.communityContent)
            }

            loadImage(item.avatar, holder.getView<SelectableRoundedImageView>(R.id.siv_header))

//            Glide.with(context)
//                .load(item.avatar)
//                .placeholder(R.mipmap.image_zanwei)
//                .into(holder.getView<SelectableRoundedImageView>(R.id.siv_header))

            holder.itemView.setOnClickListener {
                if (type == 1 || type == 2) {
                    val intent = Intent(context, LookUserDetailActivity2::class.java)
                    intent.putExtra(IntentExtra.STR_TARGET_ID, item.toUserId.toString())
                    intent.putExtra("isContact", "1")
                    context.startActivity(intent)
                } else {
                    val intent = Intent(context, CommentDetailActivity::class.java)
                    intent.putExtra("id", item.contentId.toString())
                    context.startActivity(intent)
                }
            }
        }
    }

}